package com.crm.base.service;

import com.crm.common.mapper.StorageMapper;
import com.crm.common.pojo.Storage;
import com.crm.mybatis.separation.Read;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class StorageService {

    @Autowired
    private StorageMapper storageMapper;

    @Read
    public PageInfo<Storage>  findAll(int page, int size, Storage storage) {
        PageHelper.startPage(page,size);

        storage = storage != null ? storage : new Storage();

        Example example = new Example(Storage.class);
        Example.Criteria criteria = example.createCriteria();

        if (StringUtils.isNotBlank(storage.getProductName())) {
            criteria.andLike("productName","%" + storage.getProductName() + "%");
        } else if (StringUtils.isNotBlank(storage.getWareHourse())) {
            criteria.andLike("wareHourse","%" + storage.getWareHourse() + "%");
        }

        List<Storage> storages = storageMapper.selectByExample(example);
        return new PageInfo<>(storages);
    }
}
